package br.com.skytef.bean;

import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;

import br.com.skytef.entity.BoletoEntity;
import br.com.skytef.entity.ComprovanteEntity;
import br.com.skytef.entity.DadosRetornoEntity;
import br.com.skytef.entity.ErroEntity;
import br.com.skytef.entity.InstrucaoEntity;

public class ClienteDadosRetorno {
	private DadosRetornoEntity dados = new DadosRetornoEntity();

	public DadosRetornoEntity loadData(String xmlFile) {
		//System.out.println(xmlFile);
		this.loadBoleto(xmlFile);
		this.loadErro(xmlFile);
		this.loadComprovante(xmlFile);
		this.loadIntrucoes(xmlFile);	
		
		return dados;
	}
	
	public void loadErro(String xmlFile) {
		try {
			//System.out.println(xmlFile);
			DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
			DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
			Document doc = dBuilder.parse(new InputSource(new StringReader(xmlFile)));
			
			doc.getDocumentElement().normalize();

			NodeList nList = doc.getElementsByTagName("erro");

			for (int temp = 0; temp < nList.getLength(); temp++) {

				Node nNode = nList.item(temp);

				if (nNode.getNodeType() == Node.ELEMENT_NODE) {

					Element eElement = (Element) nNode;
					
					dados.getErro().setCod(eElement.getElementsByTagName("cod").item(0).getTextContent());
					dados.getErro().setMsg(eElement.getElementsByTagName("msg").item(0).getTextContent());
					
				}
			}								
		} catch (Exception e) {
			//System.out.println("xml - erro 1");
			e.printStackTrace();
		}
	}
	
	public void loadComprovante(String xmlFile) {
		try {
			//System.out.println(xmlFile);
			DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
			DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
			Document doc = dBuilder.parse(new InputSource(new StringReader(xmlFile)));
			
			doc.getDocumentElement().normalize();
			NodeList nList = doc.getElementsByTagName("cliente");
			int size = 0;
			size = nList.getLength();
			for (int temp = 0; temp < size; temp++) {

				Node nNode = nList.item(temp);

				if (nNode.getNodeType() == Node.ELEMENT_NODE) {

					Element eElement = (Element) nNode;
						
					if(xmlFile.contains("de_comprovante"))
						dados.getComprovante().setDe_comprovante(eElement.getElementsByTagName("de_comprovante").item(0).getTextContent());					
				}
			}								
		} catch (Exception e) {
			//System.out.println("xml - erro 2");
			e.printStackTrace();
		}
	}
	
	public void loadIntrucoes(String xmlFile) {
		try {
			
			DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
			DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
			Document doc = dBuilder.parse(new InputSource(new StringReader(xmlFile)));
			
			doc.getDocumentElement().normalize();

			NodeList nList = doc.getElementsByTagName("de_instrucao");

			

			for (int temp = 0; temp < nList.getLength(); temp++) {

				Node nNode = nList.item(temp);

				if (nNode.getNodeType() == Node.ELEMENT_NODE) {
					Element eElement = (Element) nNode;
					
					int tamanho = 0;
					
					tamanho = eElement.getElementsByTagName("de_linha").getLength();					
					for (int i = 0;i < tamanho;i++){
						InstrucaoEntity intrucao = new InstrucaoEntity();
						intrucao.setInstrucao(eElement.getElementsByTagName("de_linha").item(i).getTextContent());
						dados.getInstrucoes().add(intrucao);						
					}
					
					
				}
			}								
		} catch (Exception e) {
			//System.out.println("xml - erro 3");
			e.printStackTrace();
		}
	}
	
	
	public void loadBoleto(String xmlFile) {

		try {
		
			DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
			DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
			Document doc = dBuilder.parse(new InputSource(new StringReader(xmlFile)));
			
			doc.getDocumentElement().normalize();

			NodeList nList = doc.getElementsByTagName("dados_boleto");

			for (int temp = 0; temp < nList.getLength(); temp++) {

				Node nNode = nList.item(temp);

				if (nNode.getNodeType() == Node.ELEMENT_NODE) {

					Element eElement = (Element) nNode;
									
					dados.getBoleto().setNm_banco(eElement.getElementsByTagName("nm_banco").item(0).getTextContent());
					dados.getBoleto().setNu_banco(eElement.getElementsByTagName("nu_banco").item(0).getTextContent());
					dados.getBoleto().setDv_banco(eElement.getElementsByTagName("dv_banco").item(0).getTextContent());
					dados.getBoleto().setDe_agencia_cod_cedente(eElement.getElementsByTagName("de_agencia_cod_cedente").item(0).getTextContent());
					dados.getBoleto().setDe_nosso_numero(eElement.getElementsByTagName("de_nosso_numero").item(0).getTextContent());
					dados.getBoleto().setCd_moeda(eElement.getElementsByTagName("cd_moeda").item(0).getTextContent());
					dados.getBoleto().setVl_fatura(eElement.getElementsByTagName("vl_fatura").item(0).getTextContent());
					dados.getBoleto().setDt_vencto(eElement.getElementsByTagName("dt_vencto").item(0).getTextContent());
					dados.getBoleto().setDe_local_pagto(eElement.getElementsByTagName("de_local_pagto").item(0).getTextContent());
					dados.getBoleto().setNm_cedente(eElement.getElementsByTagName("nm_cedente").item(0).getTextContent());
					dados.getBoleto().setNu_cnpj_cedente(eElement.getElementsByTagName("nu_cnpj_cedente").item(0).getTextContent());
					dados.getBoleto().setNm_sacado(eElement.getElementsByTagName("nm_sacado").item(0).getTextContent());
					dados.getBoleto().setDe_endereco_sacado(eElement.getElementsByTagName("de_endereco_sacado").item(0).getTextContent());
					dados.getBoleto().setNu_cpf_sacado(eElement.getElementsByTagName("nu_cpf_sacado").item(0).getTextContent());
					dados.getBoleto().setDt_docto(eElement.getElementsByTagName("dt_docto").item(0).getTextContent());
					dados.getBoleto().setNu_docto(eElement.getElementsByTagName("nu_docto").item(0).getTextContent());
					dados.getBoleto().setDe_especie_docto(eElement.getElementsByTagName("de_especie_docto").item(0).getTextContent());
					dados.getBoleto().setFl_aceite(eElement.getElementsByTagName("fl_aceite").item(0).getTextContent());
					dados.getBoleto().setDt_proc(eElement.getElementsByTagName("dt_proc").item(0).getTextContent());
					dados.getBoleto().setDe_uso_banco(eElement.getElementsByTagName("de_uso_banco").item(0).getTextContent());
					dados.getBoleto().setNu_carteira(eElement.getElementsByTagName("nu_carteira").item(0).getTextContent());
					dados.getBoleto().setNu_quantidade(eElement.getElementsByTagName("nu_quantidade").item(0).getTextContent());
					dados.getBoleto().setCd_barras_boleto(eElement.getElementsByTagName("cd_barras_boleto").item(0).getTextContent());			
					
				}
			}								
		} catch (Exception e) {
			//System.out.println("xml - erro 4");
			e.printStackTrace();
		}
	}	
}
